package leetcode;

public class Q0006 {
    public String convert(String s, int numRows) {
        if (numRows == 1) return s;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < numRows; i++) {
            if (i == 0 || i == numRows - 1) {
                for (int j = i; j < s.length(); j += 2 * numRows - 2) {
                    sb.append(s.charAt(j));
                }
            } else {
                for (int j = i, k = 2 * numRows - 2 - i; j < s.length(); j += 2 * numRows - 2, k += 2 * numRows - 2) {
                    sb.append(s.charAt(j));
                    if (k < s.length()) sb.append(s.charAt(k));
                }
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        Q0006 sl = new Q0006();
        String s = "PAYPALISHIRING";
        int numRows = 3;
        System.out.println(sl.convert(s, numRows));
    }
}
